//工具类
let tool = {


	changeTimeFormat: (str) => { //时间格式化
		let time_str = str.split(" ")[0].split("-")
		return `${time_str[0]}年${time_str[1]}月${time_str[2]}日 ${str.split(" ")[1].split(".")[0]}`
	},


	checkPhone: (str) => { //手机号正则校验
		if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(str))) {
			return false;
		}
		return true;
	},
	makeUp: (s) => { //补零操作
		return s < 10 ? '0' + s : s;
	},
	returnDate: () => {
		let v = {
			date: new Date(), //时间
			timeStamp: Date.parse(new Date()), //时间戳-精确到秒
			numDate: '', //2019-08-08
			strDate: '', //2019年08月08日
			time: '', //2019-08-08 12:12
			timeAll: '' //2019-08-08 12:12:12
		};
		let t = v.date;
		let y = t.getFullYear();
		let m = t.getMonth() + 1;
		let d = t.getDate();
		let h = t.getHours();
		let s = t.getMinutes();
		let min = t.getSeconds();
		v.numDate = `${tool.makeUp(y)}-${tool.makeUp(m)}-${tool.makeUp(d)}`;
		v.strDate = `${tool.makeUp(y)}年${tool.makeUp(m)}月${tool.makeUp(d)}日`;
		v.time = `${tool.makeUp(y)}-${tool.makeUp(m)}-${tool.makeUp(d)} ${tool.makeUp(h)}:${tool.makeUp(s)}`;
		v.timeAll =
			`${tool.makeUp(y)}-${tool.makeUp(m)}-${tool.makeUp(d)} ${tool.makeUp(h)}:${tool.makeUp(s)}:${tool.makeUp(min)}`;
		return v;
	},



	guid: () => { //获取全球唯一标识符
		return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
			var r = Math.random() * 16 | 0,
				v = c == 'x' ? r : (r & 0x3 | 0x8);
			return v.toString(16);
		});
	},
	setSecretKey: (data) => { //sign 短信生成密钥
		let str = ''
		for (key in data) {
			if (!str) {
				str = `${key}=${data[key]}`
			} else {
				str += `&${key}=${data[key]}`
			}
		}
		// console.log(str)
		return MD5(str).toUpperCase();
	},
	checkPhone: (str) => { //手机号正则校验
		if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(str))) {
			return false;
		}
		return true;
	},
	unique: (arr, attribute) => { //数组去重
		var new_arr = [];
		var json_arr = [];
		for (var i = 0; i < arr.length; i++) {
			if (new_arr.indexOf(arr[i][attribute]) == -1) {
				new_arr.push(arr[i][attribute]);
				json_arr.push(arr[i]);
			}
		}
		return json_arr;
	}
}


export default tool;
